home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Daemon / Unnamed1 / MPPatchStartup.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  8KB  |  310 lines

  1. #define VERSION   4
  2. #define REVISION  0
  3.  
  4. // Hotkey - screen to front
  5.  
  6. #define PATCHVER="4.0"
  7.  
  8. UBYTE SemName = "ModePro";
  9.  
  10. UBYTE Version[]     ="$VER: ModePro_Daemon "PATCHVER" "__AMIGADATE__;
  11. UBYTE ModeProPatch[]="ModePro Daemon "PATCHVER;
  12.  
  13. void ShowGUI(void);
  14. void HideGUI(void);
  15. LONG SendPrefsMsg(ULONG Command, ULONG Code, APTR Data);
  16.  
  17. /* Patch
  18.      create struct.
  19.      Load lists.
  20.      InitSem(all sems);
  21.      AddSem()
  22.       ...
  23.      quit:
  24.      RemoveSem()
  25.      ObtainSemaphore(SharedSem)
  26.  
  27.      ReplyMsgs
  28. */
  29.  
  30. /* Prefs program:
  31.     Forbid();
  32.     if(Ms=FindSem("ModePro"))
  33.     x=AttemptSemShared(SharedSem);
  34.     Permit();
  35.     
  36.     if(x)
  37.     {
  38.       if(ver & rev)
  39.       {
  40.         ObtainSem(PortSem))
  41.         Ms->PrefsPort=port;
  42.         ReleaseSem(PortSem);
  43.  
  44.         PI()
  45.         
  46.         ObtainSem(PortSem)
  47.         Ms->PrefsPort=NULL;
  48.         ReleaseSem(PortSem); 
  49.       }
  50.       else
  51.       {
  52.         wrong ver & rev
  53.       }
  54.       
  55.       ReleaseSem(SharedSem)
  56.     }
  57.     else
  58.     {
  59.       print error
  60.     }
  61. */
  62.  
  63.   
  64. struct IntuitionBase *IntuitionBase;
  65. struct Library       *AslBase,
  66.                      *CxBase,
  67.                      *DiskfontBase,
  68.                      *GadToolsBase,
  69.                      *IconBase,
  70.                      *IFFParseBase,
  71.                      *LocaleBase,
  72.                      *UtilityBase;
  73.  
  74. struct Libs MP_PatchLibs[]=
  75. {
  76.   (APTR *)&AslBase,      "asl.library",        38,
  77.   (APTR *)&CxBase,       "commodities.library",36,
  78.   (APTR *)&DiskfontBase, "diskfont.library",   36,
  79.   (APTR *)&GadToolsBase, "gadtools.library",   36,
  80.   (APTR *)&IconBase,     "icon.library",       36,
  81.   (APTR *)&IFFParseBase, "iffparse.library",   36,
  82.   (APTR *)&IntuitionBase,"intuition.library",  36,
  83.   (APTR *)&UtilityBase,  "utility.library",    36,
  84.   0,0,0
  85. };
  86.  
  87. struct MPSem MPSem;
  88. struct MsgPort *CommodityPort,*ReplyPort;
  89.  
  90. ULONG PublicSignal;
  91.  
  92. void main(int argc, char **argv)
  93. {
  94.  
  95.   if(LocaleBase=OpenLibrary("locale.library",38))
  96.     Catalog=OpenCatalog(NULL,(STRPTR)"modepro.catalog", TAG_DONE);
  97.   else
  98.     Catalog=NULL;
  99.  
  100.   if(OpenLibs(modepropatch,MP_PatchLibs))
  101.   {
  102.     if(GetArgs(argc,argv))
  103.     {
  104.     if(MPSem.PatchPort=CreateMsgPort())
  105.     {
  106.       InitSemaphore(&MPSem.PrefsSem);
  107.       MPSem.PrefsSem.ss_Link.ln_Name = SemName;
  108.       MPSem.PrefsSem.ss_Link.ln_Pri  = 0;
  109.       
  110.       InitSemaphore(&MPSem.ListSem);
  111.       MPSem.Version   = VERSION;
  112.       MPSem.Revision  = REVISION;
  113.  
  114.       MPSem.PrefsPort=0;
  115.       NewList(&MPSem.PromotionList[0]);   
  116.       NewList(&MPSem.PromotionList[1]);
  117.       NewList(&MPSem.OpenList);
  118.  
  119.       if(CommodityPort=CreateMsgPort())
  120.       { 
  121.         newbroker.nb_Pri=priority;
  122.         newbroker.nb_Port=CommodityPort;
  123.         if(broker=CxBroker(&newbroker,NULL))
  124.         {
  125.           filter=AddHotKey(broker,CommodityPort,hotkey,EVT_HOTKEY);
  126.           if(CatchPort=CreateMsgPort())
  127.           {
  128.             if(PublicSignal=AllocSignal(-1))
  129.             {
  130.               MPTask=(struct Process *)FindTask(0);
  131.               cxsig=1L<<CommodityPort->mp_SigBit;
  132.               ActivateCxObj(broker,1);
  133.               
  134.               if(!LoadFile(EnvFile))
  135.                   LoadFile(EnvArcFile);
  136.                   
  137.               Forbid();
  138.               OldCloseScreen       =SetFunction((struct Library *)IntuitionBase,-(0x42) ,(FUNCCAST)NewCloseScreen);
  139.               OldOpenScreenTagList =SetFunction((struct Library *)IntuitionBase,-(0x264),(FUNCCAST)NewOpenScreenTagList);
  140.               OldOpenScreen        =SetFunction((struct Library *)IntuitionBase,-(0xc6) ,(FUNCCAST)NewOpenScreen);
  141.               OldOpenWindowTagList =SetFunction((struct Library *)IntuitionBase,-(0x25e),(FUNCCAST)NewOpenWindowTagList);
  142.               OldOpenWindow        =SetFunction((struct Library *)IntuitionBase,-(0xcc) ,(FUNCCAST)NewOpenWindow);
  143.               OldLayoutMenusA      =SetFunction((struct Library *)GadToolsBase ,-(0x42) ,(FUNCCAST)NewLayoutMenusA);
  144.               Permit();
  145.                               
  146.               AddSemaphore(&MPSem.PrefsSem);
  147.               ProcessInput();
  148.               RemoveSemaphore(&MPSem.PrefsSem);
  149.               ObtainSemaphore(&sem);
  150.               ReleaseSemaphore(&sem);
  151.               FreeDefList(&list[0]);
  152.               FreeDefList(&list[1]);
  153.               DeleteMsgPort(CatchPort);
  154.             }
  155.           }
  156.           DeleteCxObjAll(broker);
  157.           while(cxmsg=(CxMsg *)GetMsg(CommodityPort))
  158.             ReplyMsg((struct Message *)cxmsg);
  159.         }/* if CxBroker() */
  160.         DeleteMsgPort(CommodityPort);
  161.       }
  162.       DeleteMsgPort(MPSem.PatchPort);
  163.     }
  164.     ArgArrayDone();
  165.     FreeVec(winadd);
  166.  
  167.   } 
  168.   CloseLibs(MP_Libs); 
  169.   if(LocaleBase)
  170.   {
  171.     CloseCatalog(Catalog);
  172.     CloseLibrary(LocaleBase);
  173.   }
  174. }
  175.  
  176. void ProcessInput(void)
  177. {
  178.   struct DefaultNode  *catdnode;
  179.   struct MPMessage    *mpmes;
  180.   ULONG  replysig,portsig,catchsig,signal,msgtype,pubsig,allsigs;
  181.   LONG msgid;
  182.   
  183.   catchsig=1 << CatchPort->mp_SigBit;
  184.   portsig =1 << CommodityPort->mp_SigBit;
  185.   pubsig  =1 << PublicSignal;
  186.   replysig=1 << ReplyPort->mp_SigBit;
  187.  
  188.   allsigs=replysig | pubsig | catchsig | portsig | SIGBREAKF_CTRL_C;
  189.   
  190.   while (!deinstall)
  191.   {
  192.     signal=Wait (allsigs);
  193.  
  194.     if(replysig & signal)
  195.     {
  196.       while(mpmes=(struct MPMessage *)GetMsg(ReplyPort))
  197.       {
  198.         FreeVec(mpmes);
  199.       }
  200.     }
  201.     
  202.     if(pubsig & signal)
  203.     {
  204.       SetSignal(0,pubsig);
  205.       ClosePubScreens();
  206.     }
  207.  
  208.     if(catchsig & signal)
  209.     {
  210.       while(catdnode=(struct DefaultNode *)GetMsg(CatchPort))
  211.       {
  212.         AddDNode(catdnode->Type,catdnode);
  213.       }
  214.       if(CatPop)
  215.         ShowGUI();
  216.     }
  217.         
  218.     if(portsig & signal)
  219.     {
  220.       while(cxmsg=(CxMsg *)GetMsg(mp))
  221.       {
  222.         msgid=CxMsgID(cxmsg);
  223.         msgtype=CxMsgType(cxmsg);
  224.         ReplyMsg((struct Message *)cxmsg);     
  225.         switch(msgtype)
  226.         {
  227.           case CXM_IEVENT:
  228.             switch(msgid)
  229.             {
  230.               case EVT_HOTKEY:
  231.                 ShowGUI();
  232.             }
  233.             break;
  234.           case CXM_COMMAND:
  235.             switch(msgid)
  236.             {
  237.               case CXCMD_DISABLE:
  238.                 if(menustrip && w)
  239.                   if(mi=ItemAddress(menustrip,FULLMENUNUM(0,6,NOSUB)))
  240.                   {
  241.                     mi->Flags&=(~CHECKED);
  242.                     ResetMenuStrip(w,menustrip);
  243.                   }
  244.                 active=FALSE;
  245.                 break;
  246.               case CXCMD_ENABLE:
  247.                 if(menustrip && w)
  248.                   if(mi=ItemAddress(menustrip,FULLMENUNUM(0,6,NOSUB)))
  249.                   { 
  250.                     mi->Flags|=CHECKED;
  251.                     ResetMenuStrip(w,menustrip);
  252.                   }
  253.                 active=TRUE;
  254.                 break;
  255.               case CXCMD_KILL:
  256.                 CheckToQuit();
  257.                 break;
  258.               case CXCMD_UNIQUE:
  259.               case CXCMD_APPEAR:
  260.                 ShowGUI();
  261.                 closewindow=FALSE;
  262.                 break;
  263.               case CXCMD_DISAPPEAR:
  264.                 if(w)
  265.                   HideGUI();
  266.                 closewindow=TRUE;
  267.                 break;
  268.             }
  269.             break;        
  270.         }
  271.       }
  272.     }
  273.  
  274.   }
  275.   HideGUI();
  276. }       
  277.  
  278. void ShowGUI(void)
  279. {
  280.   if(SendPrefsMsg(MP_PREFSTOFRONT,0,0)<0)
  281.     Execute("run sys:prefs/ModePro");
  282. }
  283.  
  284. void HideGUI(void)
  285. {
  286.   SendPrefsMsg(MP_PREFSCLOSE,0,0);
  287. }
  288.  
  289. /* returns -1 if prefs isn't running
  290.             0 if prefs is running but couldn't alloc mem
  291.             1 is message was sent */
  292. LONG SendPrefsMsg(ULONG Command, ULONG Code, APTR Data)
  293. {
  294.   LONG rv=-1;
  295.   struct MPMessage *mpm;
  296.  
  297.   ObtainSemaphore(&MPSem.PortSem);
  298.   if(MPSem.PrefsPort)
  299.   {
  300.     rv=0;
  301.     if(mpm=AllocVec(sizeof(struct MPMessage),MEMF_PUBLIC|MEMF_CLEAR))
  302.     {
  303.       PutMsg(MPSem.PrefsPort,(struct Message *)mpm);
  304.       rv=1;
  305.     }
  306.   }
  307.   ReleaseSemaphore(&MPSem.PortSem);
  308.   return(rv);
  309. }
  310.